using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Universal.Api.Attributes;
using Universal.Application.Contracts.Common;
using Universal.Application.Contracts.Dto.Permission;
using Universal.Application.Repositories;

namespace Universal.Api.Controllers;

[Authorize]
[ApiController]
[Route("api/[controller]")]
public class PermissionController : ControllerBase
{
    private readonly IPermissionService _permissionService;

    public PermissionController(IPermissionService permissionService)
    {
        _permissionService = permissionService;
    }

    /// <summary>
    /// 分页获取权限列表
    /// </summary>
    [HttpGet("paged")]
    [RequirePermission("permission.View")]
    public async Task<IActionResult> GetPagePermissionsAsync([FromQuery] int pageIndex, [FromQuery] int pageSize)
    {
        var result = await _permissionService.GetPermissionsPagedAsync(pageIndex, pageSize);
        return Ok(result);
    }

    /// <summary>
    /// 创建权限
    /// </summary>
    [HttpPost]
    [RequirePermission("permission.Create")]
    public async Task<IActionResult> CreatePermissionAsync([FromBody] CreatePermissionDto dto)
    {
        if (!ModelState.IsValid)
            return Ok(ApiResponse<string>.Failure(5000, "参数校验失败"));

        var result = await _permissionService.CreatePermissionAsync(dto);
        return Ok(result);
    }

    /// <summary>
    /// 编辑权限
    /// </summary>
    [HttpPut("{id}")]
    [RequirePermission("permission.Edit")]
    public async Task<IActionResult> UpdatePermissionAsync([FromRoute] Guid id, [FromBody] UpdatePermissionDto dto)
    {
        if (!ModelState.IsValid)
        {
            return Ok(ApiResponse<string>.Failure(5000, "参数校验失败"));
        }

        var result = await _permissionService.UpdatePermissionAsync(id, dto);
        return Ok(result);
    }

    /// <summary>
    /// 删除权限
    /// </summary>
    [HttpDelete("{id}")]
    [RequirePermission("permission.Delete")]
    public async Task<IActionResult> DeletePermissionAsync([FromRoute] Guid id)
    {
        var result = await _permissionService.DeletePermissionAsync(id);
        return Ok(result);
    }

    /// <summary>
    /// 启用权限
    /// <summary>
    [HttpPut("enable/{id}")]
    [RequirePermission("permission.Edit")]
    public async Task<IActionResult> EnablePermissionAsync(Guid id)
    {
        var result = await _permissionService.EnablePermissionAsync(id);
        return Ok(result);
    }

    /// <summary>
    /// 禁用权限
    /// <summary>
    [HttpPut("disable/{id}")]
    [RequirePermission("permission.Edit")]
    public async Task<IActionResult> DisablePermissionAsync(Guid id)
    {
        var result = await _permissionService.DisablePermissionAsync(id);
        return Ok(result);
    }

}